package com.jh.snap;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Servlet implementation class SnapCtrl
 */
@WebServlet(description = "snap and log", urlPatterns = { "/SnapCtrl" })
public class SnapCtrl extends HttpServlet {
    private static final long serialVersionUID = 1L;

    static final Logger logger = LoggerFactory.getLogger(SnapCtrl.class);

    /**
     * @see HttpServlet#HttpServlet()
     */
    public SnapCtrl() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset:utf-8");
        String picNum = request.getParameter("picNum");
        String picPath = request.getParameter("picPath");
        String picText = request.getParameter("picText");
        // (it == null || it.length() == 0) (it == null || it == "")
        // (it == null || it.isEmpty())
        DateFormat timeFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
        String dateStr = timeFormat.format(new Date());
        if (picNum == null || picNum.length() == 0) {
            System.out.println(picPath + picNum + picText + picText.length());
            response.getWriter().print("请先拍照！");
            return;
        } else if (picText == null || picText.length() == 0) {
            System.out.println(picPath + picNum + picText + picText.length());
            response.getWriter().print("请填写拍照日志！");
            logger.trace(dateStr + " " + picPath + picNum + "---空描述");
        } else {
            System.out.println(picPath + picNum + picText + picText.length());
            response.getWriter().print("写入日志" + picPath + picNum + picText + picText.length());
            logger.trace(dateStr + " " + picPath + picNum + "---" + picText);
        }

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
